package com.wechat.admin.service.impl;

import com.wechat.admin.exception.BusinessException;
import com.wechat.admin.exception.ErrorCode;
import com.wechat.admin.mapper.UserMapper;
import com.wechat.admin.model.dto.CurrentUserInfoDto;
import com.wechat.admin.model.dto.UserInfoDto;
import com.wechat.admin.model.vo.UserLoginInfoVo;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author:sjb
 * @CreateTime:2024-12-14
 * @Description: UserDetailUservi
 * @Version：1.0
 */
@Component
public class CustomerUserDetailService implements UserDetailsService {

    @Resource
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        CurrentUserInfoDto userInfoDto = userMapper.getLoginUserByUserName(username);
        if (userInfoDto == null) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR,"登录用户不存在");
        }
        // todo 暂时无其他校验
        return createUserLoginVo(userInfoDto);
    }

    public UserDetails createUserLoginVo(CurrentUserInfoDto userInfoDto){
        return UserLoginInfoVo.dtoToVo(userInfoDto);
    }
}
